package com.txzt.gaea.STaward.controller;

import com.txzt.base.constant.RtnConstant;
import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.STaward.dto.STAwardDTO;
import com.txzt.gaea.STaward.model.STAward;
import com.txzt.gaea.STaward.service.STAwardService;
import com.txzt.gaea.STaward.vo.STAwardVo;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.enums.FlowFlagEnum;
import com.txzt.gaea.common.page.PageInfoBT;
import com.txzt.gaea.flow.dto.NextStepDTO;
import com.txzt.gaea.flow.service.FlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/staward")
public class STAwardController extends BaseController {

    @Autowired
    private STAwardService stAwardService;
    @Autowired
    private FlowService flowService;

    @PostMapping(value = "/list")
    public RtnInfo<PageInfoBT<STAwardVo>> pageList(@RequestBody STAwardDTO stAwardDTO){
        return stAwardService.pageList(stAwardDTO);
    }

    @PostMapping(value = "/add")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo add(@RequestBody STAward stAward){
        if (null != stAward && null == stAward.getId()){
            //查重
            if (!stAwardService.addCheck(stAward)){
                return RtnInfo.error(RtnConstant.Code.PARAMS_REEOR,"存在重复数据，请检查项目名称、证书编号、成果编号");
            }
            int num = stAwardService.save(stAward);
            if (0 == num) {
                return RtnInfo.error(RtnConstant.Code.SERVER_ERROR_CODE,"数据添加失败，请联系管理员");
            }else{
                return RtnInfo.success(num);
            }
        }else {
            return RtnInfo.PARAM_MISSING;
        }
    }

    @GetMapping(value = "/item/{id}")
    public RtnInfo getInfoById(@PathVariable Long id){
        return stAwardService.getInfo(id);
    }

    @PostMapping(value = "/edit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo edit(@RequestBody STAward stAward){
        if (null != stAward && null != stAward.getId()){
            //查重
            if (!stAwardService.updateCheck(stAward)){
                return RtnInfo.error(RtnConstant.Code.PARAMS_REEOR,"存在重复数据，请检查项目名称、证书编号、成果编号");
            }
            int num = stAwardService.update(stAward);
            if (0 == num) {
                return RtnInfo.error(RtnConstant.Code.SERVER_ERROR_CODE,"数据更新失败，请联系管理员");
            }else{
                return RtnInfo.success(num);
            }
        }else {
            return RtnInfo.PARAM_MISSING;
        }
    }

    @GetMapping(value = "/delete/{id}")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo remove(@PathVariable Long id){
        return RtnInfo.success(stAwardService.delete(id));
    }

    @GetMapping(value = "/goto/check/{id}")
    public RtnInfo gotoCheck(@PathVariable Long id){
        boolean success = stAwardService.gotoCheck(id);
        if (success){
            return RtnInfo.SUCCESS;
        }else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    @PostMapping(value = "/check")
    public RtnInfo nextStep(@RequestBody NextStepDTO nextStepDTO){
        boolean next = flowService.nextStep(nextStepDTO.getBizId(), FlowFlagEnum.ACHIEVEMENT_MANAGEMENT.getCode(),
                nextStepDTO.getStatus(),nextStepDTO.getComment());
        if (!next){
            return RtnInfo.error(RtnConstant.Code.SERVER_ERROR_CODE,"审批失败，请联系管理员");
        }
        return RtnInfo.SUCCESS;
    }

    @PostMapping(value = "/test/check")
    public RtnInfo testUpdateCheck(@RequestBody STAward stAward){
        stAwardService.updateCheck(stAward);
        return RtnInfo.SUCCESS;
    }
}
